package work;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;

public class MyDatasource implements DataSource{
    LinkedList<Connection> list=new LinkedList<>();

    public MyDatasource() {
        for (int i = 0; i <5; i++) {
         Connection conn=JDBCUtils.getConnection();
         list.addLast(conn);
        }
        System.out.println("连接池初始化成功，大小是"+list.size());
    }

    @Override
    public Connection getConnection() throws SQLException {
       Connection conn=list.removeFirst();
        System.out.println("从连接池中获取对象，连接数量是"+list.size());
        return conn;
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    public void backPool(Connection conn) {
        list.addLast(conn);
        System.out.println("连接返还连接池，连接池数量是"+list.size());
    }
}
